home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / FloatVec.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  8KB  |  223 lines

  1. #ifndef FLOATVEC_H
  2. #define FLOATVEC_H
  3. #pragma once
  4.  
  5. /*
  6.  *    Declarations for Float Precision Vectors
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)FloatVec.h    2.1    8/18/89
  27.  */
  28.  
  29. /*    This code is designed to be as compatible as possible with the
  30.  *    NIH Vector classes, while preserving efficiency.  These Vectors
  31.  *    are NOT based on the NIH "Object" class, making them much
  32.  *    smaller.  They also implement reference counting, making them
  33.  *    faster. 
  34.  */
  35.  
  36. #include "vdefs.h"
  37. #include <math.h>
  38.  
  39. class istream;
  40. class ostream;
  41. class FloatVec;
  42. class DoubleVec;
  43.  
  44. class FloatBlock {
  45.   unsigned short      refs;        // Number of references
  46.   unsigned          npts;        // Number of elements
  47.   float            array[1];    // The data
  48.   friend        FloatVec;
  49. public:
  50.   FloatBlock(unsigned n);
  51.   FloatBlock(unsigned n, float val);
  52.   FloatBlock(unsigned n, float val, float by);
  53.   ~FloatBlock();
  54.  
  55.   void            add_reference()    {refs++;}
  56.   unsigned        references()    {return refs;}
  57.   float*        data()        {return array;}
  58. };
  59.  
  60. class FloatVec {
  61.   FloatBlock*        block;
  62.   float*        begin;
  63.   unsigned        npts;
  64.   int            step;
  65.  
  66.   static int        numberPerLine; // For printing
  67.   FloatVec(FloatVec&, int, unsigned, int); // For slices
  68. protected:
  69.   void            boundsErr(int);
  70.   void            boundsCheck(int);
  71.   void            lengthErr(int);
  72.   void            lengthCheck(int i)    {if(npts!=i) lengthErr(i);}
  73.   void            emptyErr(const char* fname);
  74.   void            sliceErr(unsigned, int, unsigned, int);
  75. public:
  76.   FloatVec();
  77.   FloatVec(unsigned n);
  78.   FloatVec(unsigned n, float val);
  79.   FloatVec(unsigned n, float val, float by);
  80.   FloatVec(const FloatVec& a);
  81.   FloatVec(const float* dat, unsigned n);  // Copy of dat will be made
  82.   ~FloatVec();
  83.  
  84.   // Conversion:
  85.   FloatVec(const DoubleVec& a);        // Conversion from DoubleVec
  86.   operator        DoubleVec();    // Conversion to DoubleVec
  87.  
  88.   FloatVec        slice(int start, unsigned lgt, int strider=1);
  89.   
  90.   float*        data()        {return begin;}
  91.   unsigned        length()    {return npts;}
  92.   int            stride()    {return step;}
  93.  
  94.   FloatVec&        reference(FloatVec& v);    // Reference self to v
  95.   FloatVec        deepCopy();    // copy of self with distinct instance variables 
  96.   FloatVec        copy()        {return deepCopy();} // Synonym for deepCopy()
  97.   void            deepenShallowCopy();    // Insures only 1 reference to data
  98.   void            resize(unsigned);    // Will pad with zeroes if necessary
  99.  
  100.   void            scanFrom(istream& s); // Read to eof
  101.   void            printOn(ostream& s);  // Pretty print
  102.   void            setFormatting(int);   // Change # items per line
  103.   
  104.   // Indexing:
  105.   float&        operator[](int i);    // With bounds checking
  106.   float&        operator()(int i);    // With optional bounds checking
  107.   
  108.   // Assignment:
  109.   FloatVec&        operator=(const FloatVec& v); // Must be same length as v
  110.   FloatVec&        operator=(float);
  111.   
  112.   // Arithmetic operators:
  113.   FloatVec&        operator++();
  114.   FloatVec&        operator--();
  115.   FloatVec&        operator+=(const FloatVec&);
  116.   FloatVec&        operator+=(float);
  117.   FloatVec&        operator-=(const FloatVec&);
  118.   FloatVec&        operator-=(float);
  119.   FloatVec&        operator*=(const FloatVec&);
  120.   FloatVec&        operator*=(float);
  121.   FloatVec&        operator/=(const FloatVec&);
  122.   FloatVec&        operator/=(float);
  123.   
  124.   // Friendly arithmetic operators:
  125.   friend FloatVec    operator-(const FloatVec&);
  126.   friend FloatVec    operator+(const FloatVec&);
  127.   friend FloatVec    operator*(const FloatVec&,const FloatVec&);
  128.   friend FloatVec    operator/(const FloatVec&,const FloatVec&);
  129.   friend FloatVec    operator+(const FloatVec&,const FloatVec&);
  130.   friend FloatVec    operator-(const FloatVec&,const FloatVec&);
  131.   friend FloatVec    operator*(const FloatVec&,float);
  132.   friend FloatVec    operator*(float,const FloatVec&);
  133.   friend FloatVec    operator/(const FloatVec&,float);
  134.   friend FloatVec    operator/(float,const FloatVec&);
  135.   friend FloatVec    operator+(const FloatVec&,float);
  136.   friend FloatVec    operator+(float,const FloatVec&);
  137.   friend FloatVec    operator-(const FloatVec&,float);
  138.   friend FloatVec    operator-(float,const FloatVec&);
  139.   
  140.   
  141. #ifndef NO_VECTOR_MATHFUN
  142.   // Math functions:
  143.   FloatVec    apply(mathFunTy);
  144.   friend    FloatVec    abs(const FloatVec&);
  145.   friend    FloatVec    acos(const FloatVec&);
  146.   friend    FloatVec    asin(const FloatVec&);
  147.   friend    FloatVec    atan(const FloatVec&);
  148.   friend    FloatVec    atan2(const FloatVec&,const FloatVec&);
  149.   friend    FloatVec    ceil(const FloatVec&);
  150.   friend    FloatVec    cos(const FloatVec&);
  151.   friend    FloatVec    cosh(const FloatVec&);
  152.   friend    FloatVec    cumsum(const FloatVec&);
  153.   friend    FloatVec    delta(const FloatVec&);
  154.   friend    float        dot(const FloatVec&,const FloatVec&);
  155.   friend    FloatVec    exp(const FloatVec&); 
  156.   friend    FloatVec    floor(const FloatVec&);
  157.   friend    FloatVec    log(const FloatVec&);
  158.   friend    int        max(const FloatVec&);
  159.   friend    int        min(const FloatVec&);
  160.   friend    float        mean(const FloatVec&);
  161.   friend    float        prod(const FloatVec&);
  162.   friend    FloatVec    pow(const FloatVec&,const FloatVec&);
  163.   friend    FloatVec    reverse(const FloatVec&);
  164.   friend    FloatVec    rint(const FloatVec&);
  165.   friend    FloatVec    sin(const FloatVec&);
  166.   friend    FloatVec    sinh(const FloatVec&);
  167.   friend    FloatVec    sqrt(const FloatVec&);
  168.   friend    float        sum(const FloatVec&);
  169.   friend    FloatVec    tan(const FloatVec&);
  170.   friend    FloatVec    tanh(const FloatVec&);
  171.   friend    float        variance(const FloatVec&);
  172. #endif
  173.   
  174. };
  175.  
  176. // Other (related) declarations:
  177. FloatVec    expandEven(const FloatVec&);
  178. FloatVec    expandOdd(const FloatVec&);
  179. ostream&    operator<<(ostream&, const FloatVec&);
  180. istream&    operator>>(istream&, FloatVec&);
  181.  
  182. /******************* I N L I N E S **************************/
  183.  
  184. Inline void    FloatVec::setFormatting(int i){numberPerLine = i;}
  185.  
  186. Inline void    FloatVec::boundsCheck(int i){
  187.   if(i<0 || i>npts) boundsErr(i);
  188. }
  189. Inline float&    FloatVec::operator[](int i){
  190.   boundsCheck(i); return begin[i*step];
  191. }
  192. Inline float&    FloatVec::operator()(int i) {
  193. #if BOUNDS_CHECK    
  194.   boundsCheck(i);
  195. #endif
  196.   return begin[i*step];
  197. }
  198.  
  199. Inline FloatVec    operator+(const FloatVec& a)        {return a;}
  200. Inline FloatVec    operator*(float a, const FloatVec& b)    {return b*a;}
  201. Inline FloatVec    operator+(float a, const FloatVec& b)    {return b+a;}
  202.  
  203. #ifndef NO_VECTOR_MATHFUN
  204. Inline FloatVec acos(const FloatVec& V)    { return V.apply(::acos); }
  205. Inline FloatVec asin(const FloatVec& V)    { return V.apply(::asin); }
  206. Inline FloatVec atan(const FloatVec& V)    { return V.apply(::atan); }
  207. Inline FloatVec ceil(const FloatVec& V)    { return V.apply(::ceil); }
  208. Inline FloatVec cos(const FloatVec& V)    { return V.apply(::cos); }
  209. Inline FloatVec cosh(const FloatVec& V)    { return V.apply(::cosh); }
  210. Inline FloatVec exp(const FloatVec& V)    { return V.apply(::exp); }
  211. Inline FloatVec floor(const FloatVec& V){ return V.apply(::floor); }
  212. Inline FloatVec log(const FloatVec& V)    { return V.apply(::log); }
  213. Inline FloatVec rint(const FloatVec& V)    { return V.apply(::rint); }
  214. Inline FloatVec sin(const FloatVec& V)    { return V.apply(::sin); }
  215. Inline FloatVec sinh(const FloatVec& V)    { return V.apply(::sinh); }
  216. Inline FloatVec sqrt(const FloatVec& V)    { return V.apply(::sqrt); }
  217. Inline FloatVec tan(const FloatVec& V)    { return V.apply(::tan); }
  218. Inline FloatVec tanh(const FloatVec& V)    { return V.apply(::tanh); }
  219. Inline float mean(const FloatVec& V)    { return sum(V)/V.length(); }
  220. #endif
  221.  
  222. #endif
  223.